home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 7684 / 7684.xpi / resources / fmNotifier.js < prev    next >
Text File  |  2009-11-20  |  6KB  |  199 lines

  1. /**
  2.  * Copyright (c) 2008, Jose Enrique Bolanos, Jorge Villalobos
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *
  8.  *  * Redistributions of source code must retain the above copyright notice,
  9.  *    this list of conditions and the following disclaimer.
  10.  *  * Redistributions in binary form must reproduce the above copyright notice,
  11.  *    this list of conditions and the following disclaimer in the documentation
  12.  *    and/or other materials provided with the distribution.
  13.  *  * Neither the name of Jose Enrique Bolanos, Jorge Villalobos nor the names
  14.  *    of its contributors may be used to endorse or promote products derived
  15.  *    from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  21.  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  **/
  29.  
  30. var EXPORTED_SYMBOLS = [ "FireFM.Notifier" ];
  31.  
  32. const Cc = Components.classes;
  33. const Ci = Components.interfaces;
  34. const Ce = Components.Exception;
  35.  
  36. Components.utils.import("resource://firefm/fmCommon.js");
  37. Components.utils.import("resource://firefm/fmPlayer.js");
  38.  
  39. // Notification mode values
  40. const NOTIFICATIONS_MODE_OFF   = 0;
  41. const NOTIFICATIONS_MODE_ON    = 1;
  42. const NOTIFICATIONS_MODE_FOCUS = 2;
  43.  
  44. /**
  45.  * FireFM Notifier. Displays notifications on station and player events.
  46.  */
  47. if (typeof(FireFM.Notifier) == 'undefined') {
  48. FireFM.Notifier = {
  49.  
  50.   /* Logger for this object. */
  51.   _logger : null,
  52.   /* Alerts service */
  53.   _alertsService : null,
  54.   /* Window manager */
  55.   _windowManager : null,
  56.   /* Notification mode preference object. */
  57.   _notificationModePref : null,
  58.   /* Notification title */
  59.   _title : null,
  60.  
  61.   /**
  62.    * Initializes the object.
  63.    */
  64.   init : function() {
  65.     this._logger = FireFM.getLogger("FireFM.Notifier");
  66.     this._logger.debug("init");
  67.  
  68.     // XXX: Try-catch block because the alertsService is not available on all
  69.     // operating systems.
  70.     try {
  71.  
  72.       this._alertsService =
  73.         Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService);
  74.       this._windowManager =
  75.         Cc['@mozilla.org/appshell/window-mediator;1'].
  76.           getService(Ci.nsIWindowMediator);
  77.       this._title =
  78.         FireFM.overlayBundle.GetStringFromName(
  79.           "firefm.notification.nowPlaying");
  80.  
  81.       this._notificationModePref =
  82.         FireFM.Application.prefs.get(FireFM.PREF_BRANCH + "notifications.mode");
  83.  
  84.       FireFM.obsService.addObserver(
  85.         this, FireFM.Player.TOPIC_TRACK_LOADED, false);
  86.     } catch (e) {
  87.       this._alertsService = null;
  88.     }
  89.   },
  90.  
  91.   /**
  92.    * Displays a notification using the alerts service for the given track.
  93.    * @param aTrack The track that has been loaded.
  94.    */
  95.   _notifyTrackLoaded : function(aTrack) {
  96.     this._logger.trace("_notifyTrackLoaded");
  97.  
  98.     var mode = this._notificationModePref.value;
  99.     var win = this._windowManager.getMostRecentWindow("navigator:browser");
  100.  
  101.     if (NOTIFICATIONS_MODE_ON == mode ||
  102.         (NOTIFICATIONS_MODE_FOCUS == mode &&
  103.          (!win || !win.document.hasFocus()))) {
  104.  
  105.       var image = (this._isValidImage(aTrack.imagePath)) ?
  106.                   aTrack.imagePath : "chrome://firefm/skin/logo32.png";
  107.       var trackInfo = aTrack.title + "\n" + aTrack.artist;
  108.  
  109.       if (0 < aTrack.albumTitle.length) {
  110.         trackInfo += "\n" + aTrack.albumTitle;
  111.       }
  112.  
  113.       this._alertsService.showAlertNotification(
  114.         image, this._title, trackInfo, true, aTrack.artistURL, this);
  115.     }
  116.   },
  117.  
  118.   /**
  119.    * Determines whether an image URL is valid (valid URL, file exists).
  120.    * @param aImageURL The URL of the image.
  121.    * @return True if valid, false if not valid.
  122.    */
  123.   _isValidImage : function(aImageURL) {
  124.     this._logger.trace("_isValidImage");
  125.  
  126.     let isValid = false;
  127.  
  128.     try {
  129.       if (null != FireFM.createURI(aImageURL)) {
  130.         let request =
  131.           Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
  132.             createInstance(Ci.nsIXMLHttpRequest);
  133.  
  134.         request.open("GET", aImageURL, false);
  135.         request.send(null);
  136.  
  137.         isValid = (200 == request.status);
  138.       }
  139.     } catch (e) {
  140.     }
  141.  
  142.     return isValid;
  143.   },
  144.  
  145.   /**
  146.    * Loads the given URL in the most recent window. If there are no windows
  147.    * currently opened, the URL is loaded in a new window.
  148.    * @param aURL The URL to be loaded.
  149.    */
  150.   _loadURL : function(aURL) {
  151.     this._logger.trace("_loadURL");
  152.  
  153.     var win = this._windowManager.getMostRecentWindow("navigator:browser");
  154.  
  155.     if (win) {
  156.       win.openUILinkIn(aURL, 'tab');
  157.     } else {
  158.       win =
  159.         Cc["@mozilla.org/appshell/appShellService;1"].
  160.           getService(Ci.nsIAppShellService).hiddenDOMWindow.open(aURL);
  161.     }
  162.     // TODO: Force focus somehow, but seems impossible
  163.     win.focus();
  164.   },
  165.  
  166.   /**
  167.    * Observes topic notifications.
  168.    * @param aSubject The object that experienced the change.
  169.    * @param aTopic The topic being observed.
  170.    * @param aData The data relating to the change.
  171.    */
  172.   observe : function(aSubject, aTopic, aData) {
  173.     this._logger.debug("observe");
  174.  
  175.     switch (aTopic) {
  176.       case FireFM.Player.TOPIC_TRACK_LOADED:
  177.         if (null != this._alertsService) {
  178.           let track = aSubject.wrappedJSObject;
  179.  
  180.           this._notifyTrackLoaded(track);
  181.         }
  182.  
  183.         break;
  184.  
  185.       case "alertclickcallback":
  186.         this._loadURL(aData);
  187.         break;
  188.     }
  189.   }
  190.  
  191. };}
  192.  
  193. /**
  194.  * FireFM.Notifier constructor.
  195.  */
  196. (function() {
  197.   FireFM.Notifier.init();
  198. }).apply(FireFM.Notifier);
  199.